package copyengine.screenResize
{
	import copyengine.utils.GeneralUtils;

	import flash.display.DisplayObject;
	import flash.display.Stage;
	import flash.events.Event;

	public final class CEScreenResizeManger
	{
		/**
		 *当前坐标方向不随着resize而改变(比如Pin在左边,此时X坐标不随Resize移动而移动)
		 */
		public static const RESIZE_NOT_MOVE:int = 0;

		/**
		 *resize多少则移动多少,(比如Pin在右边,此时resize offset多少则移动X方向多少)
		 */
		public static const RESIZE_MOVE_OFFSET:int = 1;

		/**
		 *按resize的百分比移动,(比如Pin在底下中部,此时需要一直保持居中,则offset的百分比是多少则移动多少)
		 */
		public static const RESIZE_MOVE_PERCENT:int = 2;

		/**
		 * resize时候缩放Mc的Width or Height
		 */
		public static const RESIZE_SCALE_OFFEST:int = 1;

		/**
		 *Resize时候按照比例缩放
		 */
		public static const RESIZE_SCALE_PERCENT:int = 2;

		/**
		 *Resize时候缩放Offset的一半
		 * </br>BottomUI中用需要用两块FillMc填满UI由于Resize空出来的部分，
		 * 此时两块FillMc应该个扩大Offset的一半
		 */
		public static const RESIZE_SCALE_HALF_OFFSET:int = 3;

		/**
		 * resize时候不缩放
		 */
		public static const RESIZE_NOT_SCALE:int = 4;

		/**
		 *GUI文件中的宽高设置,也就是在开发期间摆放在FLA文件里面的位置,
		 * 用于计算当Resize时候应该进行的偏移
		 */
		private static const GUI_FILE_STAGE_WIDTH:int = 760;
		private static const GUI_FILE_STAGE_HEIGH:int = 700

		private static var _instance:CEScreenResizeManger;

		public static function get instance():CEScreenResizeManger
		{
			if (_instance == null)
			{
				_instance = new CEScreenResizeManger();
			}
			return _instance;
		}

		private var stage:Stage;
		private var allScreenResizeListenerVector:Vector.<IScreenResizeListener>;

		public function CEScreenResizeManger()
		{
		}

		public function initialize(_stage:Stage):void
		{
			stage = _stage;

			allScreenResizeListenerVector = new Vector.<IScreenResizeListener>();
			addListener();
		}

		public function getCurrentStageWidth():int
		{
			return stage.stageWidth;
		}

		public function getCurrentStageHeight():int
		{
			return stage.stageHeight;
		}

		public function addScreenResizeListener(_listener:IScreenResizeListener):void
		{
			allScreenResizeListenerVector.indexOf(_listener) < 0 && allScreenResizeListenerVector.push(_listener);
		}

		public function removeScreenResizeListener(_listener:IScreenResizeListener):void
		{
			GeneralUtils.removeElementFromArrayOrVector(_listener, allScreenResizeListenerVector);
		}

		public function addAutoResizeUIComponent(_target:DisplayObject,
												 _xDirResizeWay:int,
												 _yDirResizeWay:int,
												 _xDirScaleWay:int = RESIZE_NOT_SCALE,
												 _yDirScaleWay:int = RESIZE_NOT_SCALE):void
		{
			var autoResizeListenerWarp:CEUIAutoResizeListenerWarper = new CEUIAutoResizeListenerWarper();
			autoResizeListenerWarp.setTarget(_target, _xDirResizeWay, _yDirResizeWay, _xDirScaleWay, _yDirScaleWay);
			allScreenResizeListenerVector.push(autoResizeListenerWarp);

			var offsetWidth:int = stage.stageWidth - GUI_FILE_STAGE_WIDTH;
			var offsetHeight:int = stage.stageHeight - GUI_FILE_STAGE_HEIGH;
			var offsetXPercent:Number = stage.stageWidth / GUI_FILE_STAGE_WIDTH;
			var offsetYPercent:Number = stage.stageHeight / GUI_FILE_STAGE_HEIGH;
			autoResizeListenerWarp.onScreenResize(stage.stageWidth,
												  stage.stageHeight,
												  offsetWidth,
												  offsetHeight,
												  offsetXPercent,
												  offsetYPercent);

		}

		public function removeAutoResizeUIComponent(_target:DisplayObject):void
		{
			for each (var listener:IScreenResizeListener in allScreenResizeListenerVector)
			{
				if (listener is CEUIAutoResizeListenerWarper && (listener as CEUIAutoResizeListenerWarper).getTarget() == _target)
				{
					removeScreenResizeListener(listener);
					break;
				}
			}
		}


		private function addListener():void
		{
			GeneralUtils.addTargetEventListener(stage, Event.RESIZE, onStageResize);
		}

		private function removeListener():void
		{
			GeneralUtils.removeTargetEventListener(stage, Event.RESIZE, onStageResize);
		}

		private function onStageResize(e:Event):void
		{
			var offsetWidth:int = stage.stageWidth - GUI_FILE_STAGE_WIDTH;
			var offsetHeight:int = stage.stageHeight - GUI_FILE_STAGE_HEIGH;
			var offsetXPercent:Number = stage.stageWidth / GUI_FILE_STAGE_WIDTH;
			var offsetYPercent:Number = stage.stageHeight / GUI_FILE_STAGE_HEIGH;

			for each (var listener:IScreenResizeListener in allScreenResizeListenerVector)
			{
				listener.onScreenResize(stage.stageWidth,
										stage.stageHeight,
										offsetWidth,
										offsetHeight,
										offsetXPercent,
										offsetYPercent);
			}
		}

	}
}
